package com.homedo;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.homedo.distributed.id.client.gen.IdGen;
import org.springframework.util.StopWatch;

import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

public class IdGenTest4 {
    static AtomicInteger dd = new AtomicInteger(0);

    public static void main(String[] args) throws InterruptedException {
        List<Thread> tt = Lists.newArrayList();

        for (int i = 0; i < 50; i++) {
//            Thread a = new IdThrd("test1" + i % 2);
            Thread a = new IdThrd("test1" + i);
            a.setDaemon(true);
            a.start();

            tt.add(a);
        }

        for (Thread thread : tt) {
            thread.join();
        }
        System.out.println(dd.get());
    }

    static class IdThrd extends Thread {
        public String bizTag;

        public IdThrd(String bizTag) {
            this.bizTag = bizTag;
        }

        @Override
        public void run() {
            Set<String> ss = Sets.newHashSet();

            for (int i = 0; i < 1000; i++) {
//                if (new Random().nextInt(10) % 2 == 0) {
                try {
                    TimeUnit.MILLISECONDS.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
//                }

                StopWatch stopWatch = new StopWatch();
                stopWatch.start();

                try {
                    String id = IdGen.regularity(bizTag, 500);
                    stopWatch.stop();

                    System.out.println("===getTotalTimeMillis=" + stopWatch.getTotalTimeMillis());

//                    System.out.println(Thread.currentThread() + "====" + id);
                    ss.add(id);
                    dd.incrementAndGet();
                } catch (Throwable ex) {
                    System.out.println("error------" + ex.getMessage());
                }
            }

            System.out.println(ss.size());
        }
    }
}
